package com.hooya.fa.eu.biz.dao.manager;

import com.hooya.fa.eu.api.bo.stk.BuyRecordBO;
import com.hooya.fa.eu.api.bo.stk.ReturnedRecordBO;
import com.hooya.fa.eu.biz.dao.entity.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.Optional;

/**
 * @Description 1
 * @Author : Zongz
 * @Copyright Copyright (c) 豪雅集团
 * @Create 2024/2/27
 * @Version 1.0.0
 */
@Slf4j
@Component
@AllArgsConstructor
public class GeneratorEntityManager {

    public TempKcjlDaily genTempKcjlDaily(String cpbh, String belongs, String place, int sl, String date) {
        return TempKcjlDaily.builder().belongs(belongs).place(place)
                .cpbh(cpbh).sl(sl).date(date).build();
    }

    public TempJcjl genTempJcjl(BuyRecordBO jc, String belongs) {
        return TempJcjl.builder().cpbh(jc.getCpbh()).cgsl(jc.getCgsl())
                .place(jc.getPlace()).belongs(belongs).hthm(jc.getHthm())
                .htrq(jc.getHtrq()).sheetnumber(jc.getSheetNumber()).build();
    }

    public TempZyjl genTempZyjl(BuyRecordBO zy, int num, String zcBelongs, String zlBelongs,
                                String zcOrigin, String zlOrigin) {
        return TempZyjl.builder().cpbh(zy.getCpbh()).zcck(zy.getZcck()).zlck(zy.getZlck())
                .cgsl(num).hthm(zy.getHthm()).htrq(zy.getHtrq()).frombelongs(zcBelongs)
                .tobelongs(zlBelongs).fromcountry(zcOrigin).tocountry(zlOrigin)
                .sheetnumber(zy.getSheetNumber()).build();
    }

    public <T> TradeRecord genTradeRecord(T record, int num, String zcBelongs, String zlBelongs,
                                          String zcOrigin, String zlOrigin, String type) {

        // 获取record对象的类
        Class<?> recordClass = record.getClass();
        TradeRecord tradeRecord = TradeRecord.builder()
                .mmsl(num).fromcompany(zcBelongs).tocompany(zlBelongs)
                .fromcountry(zcOrigin).tocountry(zlOrigin).type(type).build();
        getFieldSafely(recordClass, "sheetNumber").ifPresent(f -> {
            try {
                tradeRecord.setSheetnumber((String) f.get(record));
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e.getMessage());
            }
        });
        getFieldSafely(recordClass, "place").ifPresent(f -> {
            try {
                tradeRecord.setPlace((String) f.get(record));
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e.getMessage());
            }
        });
        //xsjl special deal
        if (StringUtils.isBlank(tradeRecord.getPlace())) {
            getFieldSafely(recordClass, "chck").ifPresent(f -> {
                try {
                    tradeRecord.setPlace((String) f.get(record));
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e.getMessage());
                }
            });
        }
        getFieldSafely(recordClass, "hthm").ifPresent(f -> {
            try {
                tradeRecord.setZyhthm((String) f.get(record));
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e.getMessage());
            }
        });
//        getFieldSafely(recordClass, "htrq").ifPresent(f -> {
//            try {
//                tradeRecord.setDate((String) f.get(record));
//            } catch (IllegalAccessException e) {
//                throw new RuntimeException(e.getMessage());
//            }
//        });
        getFieldSafely(recordClass, "cpbh").ifPresent(f -> {
            try {
                tradeRecord.setCpbh((String) f.get(record));
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e.getMessage());
            }
        });
        getFieldSafely(recordClass, "zcck").ifPresent(f -> {
            try {
                tradeRecord.setZcck((String) f.get(record));
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e.getMessage());
            }
        });
        getFieldSafely(recordClass, "zlck").ifPresent(f -> {
            try {
                tradeRecord.setZlck((String) f.get(record));
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e.getMessage());
            }
        });
        return tradeRecord;
    }

    public static Optional<Field> getFieldSafely(Class<?> clazz, String fieldName) {
        try {
            Field field = clazz.getDeclaredField(fieldName);
            field.setAccessible(true); // 如有必要，设置可访问性
            return Optional.of(field);
        } catch (NoSuchFieldException e) {
            return Optional.empty();
        }
    }

    public TempThjl gentempThjl(ReturnedRecordBO th, String belongs, String type, int num) {
        return TempThjl.builder().cpbh(th.getSku()).place(th.getPlace()).sl(num).
                company(th.getCompany()).belongs(belongs).flag(type).date(th.getOrderDate()).build();
    }
}
